/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.mt.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.pig4cloud.pig.mt.common.api.handler.JSONObjectTypeHandler;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;

import java.util.Date;

/**
 * 微信消息
 *
 * @author JLM
 * @date 2021-03-05 11:45:09
 */
@Data
@TableName("wx_msg")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "微信消息")
public class WxMsg extends Model<WxMsg> {
private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(type = IdType.ASSIGN_UUID)
    @ApiModelProperty(value="主键")
    private String id;
    /**
     * appid
     */
    @ApiModelProperty(value="appid")
    private String appid;
    /**
     * 微信用户ID
     */
    @ApiModelProperty(value="微信用户ID")
    private String openid;
    /**
     * 消息方向
     */
    @ApiModelProperty(value="消息方向")
    private byte inOut;
    /**
     * 消息类型
     */
    @ApiModelProperty(value="消息类型")
    private String msgType;
    /**
     * 消息详情
     */
    @ApiModelProperty(value="消息详情")
	@TableField(typeHandler = JSONObjectTypeHandler.class)
	private JSONObject detail;
    /**
     * 创建时间
     */
    @ApiModelProperty(value="创建时间")
    private Date createTime;

	public static class WxMsgInOut {
		static final byte IN = 0;
		static final byte OUT = 1;
	}

	public WxMsg() {
	}

	public WxMsg(WxMpXmlMessage wxMessage) {
		this.openid = wxMessage.getFromUser();
		this.appid = WxMpConfigStorageHolder.get();
		this.inOut = WxMsgInOut.IN;
		this.msgType = wxMessage.getMsgType();
		this.detail = new JSONObject();
		Long createTime = wxMessage.getCreateTime();
		this.createTime = createTime == null ? new Date() : new Date(createTime * 1000);
		if (WxConsts.XmlMsgType.TEXT.equals(this.msgType)) {
			this.detail.put("content", wxMessage.getContent());
		} else if (WxConsts.XmlMsgType.IMAGE.equals(this.msgType)) {
			this.detail.put("picUrl", wxMessage.getPicUrl());
			this.detail.put("mediaId", wxMessage.getMediaId());
		} else if (WxConsts.XmlMsgType.VOICE.equals(this.msgType)) {
			this.detail.put("format", wxMessage.getFormat());
			this.detail.put("mediaId", wxMessage.getMediaId());
		} else if (WxConsts.XmlMsgType.VIDEO.equals(this.msgType) ||
				WxConsts.XmlMsgType.SHORTVIDEO.equals(this.msgType)) {
			this.detail.put("thumbMediaId", wxMessage.getThumbMediaId());
			this.detail.put("mediaId", wxMessage.getMediaId());
		} else if (WxConsts.XmlMsgType.LOCATION.equals(this.msgType)) {
			this.detail.put("locationX", wxMessage.getLocationX());
			this.detail.put("locationY", wxMessage.getLocationY());
			this.detail.put("scale", wxMessage.getScale());
			this.detail.put("label", wxMessage.getLabel());
		} else if (WxConsts.XmlMsgType.LINK.equals(this.msgType)) {
			this.detail.put("title", wxMessage.getTitle());
			this.detail.put("description", wxMessage.getDescription());
			this.detail.put("url", wxMessage.getUrl());
		} else if (WxConsts.XmlMsgType.EVENT.equals(this.msgType)) {
			this.detail.put("event", wxMessage.getEvent());
			this.detail.put("eventKey", wxMessage.getEventKey());
		}
	}

	public static WxMsg buildOutMsg(String msgType, String openid, JSONObject detail) {
		WxMsg wxMsg = new WxMsg();
		wxMsg.appid = WxMpConfigStorageHolder.get();
		wxMsg.msgType = msgType;
		wxMsg.openid = openid;
		wxMsg.detail = detail;
		wxMsg.createTime = new Date();
		wxMsg.inOut = WxMsgInOut.OUT;
		return wxMsg;
	}

}
